/**
 * 违规信息
 */
Ext.define('Module.wg.WgInfoWindow', {
	extend : 'Ext.window.Window',
	layout: 'border',
	requires: ['Sh.ux.VideoPlayer'],
	id:'wgInfoWindow_id',
	border: false,
	config: {
		player: undefined,
		videoId:'',
		synpath:'',
		bjs: [],
		bjid: undefined
	},
	initComponent : function() {
		var me = this;

		me.on("boxready",function(){
			me.getRenWuInfo(me.getVideoId());
			me.ruanTask();
		});
		
		
		var westPanel = me.westPanel =Ext.create('Ext.panel.Panel',{
			layout:'column',
			region: 'center',
			height:800,
			width:'60%',
			border:true,
			defaults: {
				border:false
			},
			items: [{
				xtype: 'form',
				'margin':'3 0 0 4',
				columnWidth:.6,
				style:{'border-right':'1px solid #FFFFFF'},
				items: [{
						title:"违规视频",
						xtype: 'player',
						region: 'center',
						border:true,
						autoPlay: true,
						height:400,
						width:'100%',
						vdata:{id:me.getVideoId()},
						src: me.getSynpath(),
						id:me.id+"-wgPlay",
						onloadeddata:function(){
							me.updateMd(westPanel.down('player'),me.getVideoId());
						}
					},{
				    	xtype:"fieldset",
				    	height:160,
						style: {
							'margin':'6 0 0 0',
							borderColor: '#14a296',
				            backgroundColor:'#DBF2EF',
					        'border-radius': '5px'
				        },
				    	defaults: {
				    		labelWidth: 65	
						},
						border:true,
				        items:[{
								fieldLabel: '任务内容',
								xtype: 'displayfield',
								id: me.id+'content'
							},{
								fieldLabel: '变电站名称',
								xtype: 'displayfield',
								id: me.id+'bdzName'
							},{
								fieldLabel: '班组名称',
								xtype: 'displayfield',
								id: me.id+'bzName'
							},{
								fieldLabel: '视频名称',
								xtype: 'displayfield',
								id: me.id+'vname'
							},{
								fieldLabel: '标记数量',
								xtype: 'displayfield',
								id: me.id+'bjNumber'
							}]
					}]
			},
			{
				xtype: 'form',
				height:800,
				'margin':'3 3 0 4',
				columnWidth:.4,
				items: [{
					title:'详细信息',
					xtype: 'panel',
					border:true,
					items: {
					    xtype: 'box', //或者xtype: 'component',  
					    width: '100%', //图片宽度  
					    id:me.id+"image",
//					    border:true,
					    height:374
				    }
				},{
			    	xtype:"fieldset",
			    	height:160,
					style: {
						'margin':'5 0 0 0',
						borderColor:'#EE7979',
			            backgroundColor:'#FBDFDF',
				        'border-radius': '5px'
			        },
			    	defaults: {
			    		labelWidth: 60	
					},
			        border:true,
			        items:[{
							fieldLabel: '违规类型',
							xtype: 'displayfield',
							id: me.id+'syjs-wgmc'
						},{
							fieldLabel: '发生时间',
							xtype: 'displayfield',
							id: me.id+'syjs-wgsj'
						},{
							fieldLabel: '详细描述',
							xtype: 'displayfield',
							id: me.id+'syjs-wgxq'
						}]
				}] 
			}]
		});
		
		
		
		

		me.items = [westPanel];

		me.callParent();
	},
	getRenWuInfo:function(videoId){//视频id
		var me=this;
		 Ext.Ajax.request({
			 url: 'wgSyjsAction/getWgVideoList.do',
             params:{
            	 videoId:videoId
             },
             success: function (response, options) {
            	 var data = Ext.decode(response.responseText);
            	 Ext.getCmp(me.id+'content').setValue(data[0].content);
				 Ext.getCmp(me.id+'vname').setValue(data[0].vname);
				 Ext.getCmp(me.id+'bjNumber').setValue(data[0].bjNumber);
				 Ext.getCmp(me.id+'bdzName').setValue(data[0].bdzName);
				 Ext.getCmp(me.id+'bzName').setValue(data[0].bzName);
             }
         });
	},
	/**
	 * 更新违规标记信息
	 */
	updateWgBj:function(videoId,wgtime){//视频id，违规时间
		var me=this;
		 Ext.Ajax.request({
			 url: 'wgVideoAction/getWgBjById.do',
             params:{
            	 videoId:videoId,
            	 wgtime:wgtime
             },
             success: function (response, options) {
            	 var bj = Ext.decode(response.responseText);
            	 Ext.getCmp(me.id+'syjs-wgmc').setValue(bj.title);
				 Ext.getCmp(me.id+'syjs-wgsj').setValue(me.formatSeconds(bj.wgtime));
				 Ext.getCmp(me.id+'syjs-wgxq').setValue(bj.describe);
             }
         });
	},
	formatSeconds : function(value){//将秒转换为时间
		var theTime = parseInt(value);// 秒
		var theTime1 = 0;// 分
		var theTime2 = 0;// 小时
	    if(theTime > 60) {
	        theTime1 = parseInt(theTime/60);
	        theTime = parseInt(theTime%60);
	            if(theTime1 > 60) {
	            theTime2 = parseInt(theTime1/60);
	            theTime1 = parseInt(theTime1%60);
	            }
	    }
        var result = ""+parseInt(theTime);
        if(theTime1 > 0) {
        	result = ""+parseInt(theTime1)+":"+result;
        }
        if(theTime2 > 0) {
        	result = ""+parseInt(theTime2)+":"+result;
        }
        var r= result.indexOf(":");
        if(r==-1){
        	result="0:"+result;
        }
        return result;
    },
    ruanTask: function(){
		var me = this;
		Ext.TaskManager.start({//定时器
			interval: 500,
			run: function(){
				try {
					var p2 = me.westPanel.down('player');
					if(p2){
						var time = p2.getCurrentTime();
						var bjs = me.getBjs();
						for ( var i = bjs.length-1; i >= 0; i--) {
							var bj = bjs[i];
							if(bj.wgtime < time){
								if(me.bjid != bj.id){
									Ext.getCmp(me.id+"image").update({
										html: '<img width="100%" height="100%" src="wgVideoAction/getWgImg.do?wgtime='+bj.wgtime+'&videoId='+bj.videoId+'"/>'
									});
									 Ext.getCmp(me.id+'syjs-wgmc').setValue(bj.title);
									 Ext.getCmp(me.id+'syjs-wgsj').setValue(me.formatSeconds(bj.wgtime));
									 Ext.getCmp(me.id+'syjs-wgxq').setValue(bj.describe);
									me.bjid = bj.id;
								}
								
								break;
							}
						}
					}
					
				} catch (e) {
				}
			}
		});
	},
    updateMd: function(c,vindex){//c:player、vindex:视频id
		var me = this;
		$('div[md=1]',c.el.dom).remove();
		var prbar = c.down('progressbar');
		$(prbar.el.dom).append("<div md=1 style='filter: alpha(opacity=50); opacity:0.5;postion:relative;width:100%;height:100%;'></div>");
		Ext.Ajax.request({
		    url: 'wgVideoAction/getWgBj.do',
		    params: {videoId:vindex},
		    success: function(response){
		        var text = response.responseText;
		        var data = Ext.decode(text);
		        me.setBjs(data);
		        for ( var key in data) {
		        	me.renderMd(c,data[key]);
				}
		    }
		});
	},
	renderMd: function(c,data){//描点
		var me=this;
		var time = data.wgtime,text=data.text;
		var p = c;
		var prbar = p.down('progressbar');
		var left = time*(prbar.getWidth()/p.getAllTime());
//		c.goToTime(parseInt(time));
		var md = $('<div></div>').css('display','inline-block').css('position','absolute').css('opacity',100).css('filter','alpha(opacity=100)')
		.css('background','red').css('height','100%').css('cursor','pointer').css('margin-left',left).width(3)
		.data('data',data).click(function(e){
			var d = $(e.target).data('data');
			Ext.getCmp('wgInfoWindow_id').updateWgBj(d.videoId,d.wgtime);
			Ext.getCmp(me.id+"image").update({
				html:'<img width="100%" height="100%" src="wgVideoAction/getWgImg.do?videoId='+d.videoId+'&wgtime='+d.wgtime+'"/>'
				});
		})
		.bind("contextmenu",function(e){
			Ext.create('Ext.menu.Menu', {
			    width: 100,
			    margin: '0 0 10 0',
			    floating: true,  // usually you want this set to True (default)
			    items: [{
			    	iconCls: 'icon-delete',
			        text: '删除标记',
			        handler: function(){
			        	Ext.Ajax.request({
						    url: 'wgVideoAction/delMd.do',
						    params: {id: $(e.target).data('data').id},
						    success: function(response){
						        var text = response.responseText;
						        var data = Ext.decode(text);
						        if(data.rst){
						        	me.updateMd(Ext.getCmp(me.id+"-wgPlay"),$(e.target).data('data').videoId);
						        }
						    }
						});
			        }
			    }]
			}).showAt([e.pageX,e.pageY-45]);
			
	        return false;   
	    });
		$('div[md=1]',c.el.dom).append(md);
		Ext.QuickTips.register({
			target: md[0],
			title: '描点,'+time,
			text: text,//具体要提示的东西
			width: 200,
			dismissDelay: 100000
		});
	}
});
